package com.fenghongzhang.day003;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btn1(View view) {
        //负责对通知,发送, 取消.更新.
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //大图标
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setContentTitle("标题").setContentText("内容");
        builder.setWhen(System.currentTimeMillis());


        //设置通知课弹出
        builder.setPriority(Notification.PRIORITY_MAX);
        builder.setDefaults(Notification.DEFAULT_ALL); //声音,灯光,震动

        Notification build = builder.build();

        manager.notify(1,build);
    }

    public void btn2(View view) {
        //负责对通知,发送, 取消.更新.
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //大图标
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setContentTitle("标题1").setContentText("内容1");
        builder.setWhen(System.currentTimeMillis());


        Intent intent = new Intent(this,Main2Activity.class);
        PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(activity);
        builder.setAutoCancel(true);

        //设置通知课弹出
        builder.setPriority(Notification.PRIORITY_MAX);
        builder.setDefaults(Notification.DEFAULT_ALL); //声音,灯光,震动

        Notification build = builder.build();

        manager.notify(2,build);
    }

    public void btn3(View view) {
        final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        final Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);

        builder.setContentTitle("下载");
        builder.setContentText("正在下载");

        builder.setProgress(100,0,false);

        //定时器
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int index = 0;
            @Override
            public void run() {
                Log.i(TAG, "run: ");
                builder.setProgress(100,index,false);
                index += 10;
                manager.notify(2,builder.build());

                if(index >= 100){
                    builder.setContentText("正在安装");
                    builder.setProgress(0,0,true);
                    manager.notify(2,builder.build());
                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    manager.cancel(2); //取消通知
                    timer.cancel();

                }

            }
        },0,1000);


        builder.setAutoCancel(true);
        Notification build = builder.build();

        manager.notify(2,build);
    }

    public void btn4(View view) {
        //负责对通知,发送, 取消.更新.
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);


        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_layout);

        remoteViews.setTextViewText(R.id.hehe,"呵呵呵");
        remoteViews.setImageViewResource(R.id.img,R.mipmap.ic_launcher_round);

        Intent intent = new Intent(this, Main2Activity.class);
        PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
        remoteViews.setOnClickPendingIntent(R.id.click,activity);


        builder.setCustomContentView(remoteViews); //24

//        builder.setContent(remoteViews);//22

        Notification build = builder.build();

        manager.notify(1,build);
    }

    public void btn5(View view) {
        //负责对通知,发送, 取消.更新.
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setGroup("hehe");
        builder.setContentTitle("标题").setContentText("内容");


        Notification.Builder builder1 = new Notification.Builder(this);
        builder1.setSmallIcon(R.mipmap.ic_launcher);
        builder1.setGroup("hehe");
        builder1.setContentTitle("标题1").setContentText("内容1");

        Notification.Builder builder2 = new Notification.Builder(this);
        builder2.setSmallIcon(R.mipmap.ic_launcher);
        builder2.setGroup("hehe");
        builder2.setGroupSummary(true);
        builder2.setContentTitle("标题2").setContentText("内容2");



        manager.notify(1, builder.build());
        manager.notify(2, builder1.build());
        manager.notify(3,builder2.build());


    }

    public void btn6(View view) {
        //负责对通知,发送, 取消.更新.
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);

        //大图样式
//        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
//        bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));


        Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
        inboxStyle.addLine("小明");
        inboxStyle.addLine("大明");
        inboxStyle.addLine("猴子");

        //列表或者大图
        builder.setStyle(inboxStyle);
//        builder.setStyle(bigPictureStyle);

        builder.setVisibility(Notification.VISIBILITY_PUBLIC);

        //设置通知课弹出
        builder.setPriority(Notification.PRIORITY_MAX);
        builder.setDefaults(Notification.DEFAULT_ALL); //声音,灯光,震动


        manager.notify(1, builder.build());

    }
}
